<?php
/**
 * Data Model
 *
 * @property Data $ParentData
 * @property Data $ChildData
 */
class WxMsg extends WechatAppModel {

/**
 * Use table
 *
 * @var mixed False or table name
 */
	public $useTable = false;
	
	public $linkSendTplMsg = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
	
	public function TplMsg($openid,$tpl_id,$data){
	    $option = array();
	    
	    App::import('Vendor', 'Wechat.WxTpl', array('file' => 'wx_tpl_msg.php'));
	    $WxTpl = new WxTpl();
	    $tpl = $WxTpl->getTpl($tpl_id,$data);

	    if(!$tpl){
	        return false;
	    }
	    $msg = array(
	        "touser"=>$openid,
	        "template_id"=>$tpl_id,
	        "url"=>$tpl['url'],
	        "topcolor"=>$tpl['topcolor'],
	        "data"=>$tpl['data']
	    );
	    $msg = json_encode($msg);
	    $res = $this->sendTplMsg($msg);
	    $res = json_decode($res,true);
	    if(isset($res['errmsg'])&&$res['errmsg'] == 'ok'){
	        return true;
	    }else{
	        return false;
	    }
	}
	
	public function sendTplMsg($data){
	    App::import('Model','Wechat.Wechat');
        $Wechat = new Wechat();
	    $url = $this->linkSendTplMsg.$Wechat->access_token();
	    return $this->post($url,$data);
	}
	
}
